/**
 * Created by tang on 2018/3/27.
 */
import {
  postCarouselAdd, postCarouselDel, postCarouselGet, postCarouselList, postCarouselUpdate,postCarouselInfo,
  postNotTable,postNotTableInfo,postCensorWords,postCensorWordsUp
} from '../services/banner';
import { message } from 'antd';


export default {
  namespace: 'banner',

  state: {
    dataList: [],

    loading: true,
    page: 1,
    total: 0,
    pageSize: 8,
    id: 0,

    //字段
    title: '', //标题
    type: 2, //新闻类型
    sort: 0, //排序
    appendix: '', //文件名
    imgType:1,
    url:"",
    submitting: false,
    //图片上传
    previewVisible: false,
    previewImage: '',
    fileList: [],
    fileLists: [],
  },

  effects: {
    //列表
    *fetch({ payload, callback }, { call, put,select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      var params = {};
      var date = yield select(state => state.banner);
      if (payload){
        params = payload;
      }
      params["page"] = date.page;
      params["pageSize"] = date.pageSize;
      params["imgType"] = date.imgType;
      const data = yield call(postCarouselList, params);
      if (data.recode == 200) {
        yield put({
          type: 'save',
          payload: {dataList: data.body.dataList, total: data.body.total}
        });
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });

      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },
    //删除
    *remove({ payload, callback }, { call, put, select }) {
      var myState = yield select(state => state.banner);
      yield put({
        type: 'changeLoading',
        payload: true,
      });

      const data = yield call(postCarouselDel, payload);
      if (data){
        if (data.recode === 200){
          yield put({
            type: 'fetch',
            //payload: {id: id},
          })
        } else if (data.recode == 9000){
          // message.error("您的权限不足");
        } else if (data.recode == 1001){
          // window.location.hash = "/user/login"
        } else {
          // message.error('网络请求失败!');
        }
      } else {
        // message.error('网络连接失败');
      }

      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },
    //添加
    *add({ payload }, { call ,put }) {
      yield put({
        type: 'changeLoading',
        payload: true
      });

      const data = yield call(postCarouselAdd, payload);
      if (data.recode == 200){

        yield put({
          type: "clear"
        });
        window.location.hash = "/system/banner";
      } else if (data.recode == 9000){
        message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }

      yield put({
        type: 'save',
        payload: {submitting: false}
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    //调到修改页
    *toUpdate({ callback, payload }, { call, put }){
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const data = yield call(postCarouselGet, payload);
      //console.log(data);

      if (data.recode == 200) {
        yield put({
          type: 'save',
          payload: data.body.dataList
        });
        yield put({
          type: 'save',
          payload: {fileList: [{
            uid: -1,
            name: data.body.dataList.img,
            status: 'done',
            url: data.body.dataList.img,
          }]}
        });
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }

      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },
    //修改
    *update({ payload }, { call ,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const data = yield call(postCarouselUpdate, payload);
      yield put({
        type:'save',
        payload:payload
      });
      if (data.recode == 200){
        yield put({
          type: "clear"
        });
        window.location.hash = "/system/banner";
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }

      yield put({
        type: 'save',
        payload: {submitting: false}
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    //详情
    *info({ callback, payload }, { call, put }){
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const data = yield call(postCarouselInfo, payload);
      if (data.recode == 200) {
        yield put({
          type: 'save',
          payload: data.body.dataList
        });
        //console.log("payload",data.body.newsInfo)
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },
    //通知
    *notTable({ callback, payload }, { call, put }){
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const data = yield call(postNotTable, payload);
      if (data.recode == 200) {
        yield put({
          type:'notTableInfo'
        })
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },

    //上传敏感文件
    *censorWordsUp({ payload }, { call ,put }) {
      yield put({
        type: 'changeLoading',
        payload: true
      });

      const data = yield call(postCensorWordsUp, payload);
      if (data.recode == 200){
        message.success('上传成功!');
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }

      yield put({
        type: 'save',
        payload: {submitting: false}
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    //下载敏感字符文件
    *censorWords({ payload }, { call ,put }) {

      yield put({
        type: 'changeLoading',
        payload: true
      });
      const data = yield call(postCensorWords, payload);
      if (data.recode == 200){
        window.location.href = data.body.censorWords.systemValue;
      } else if (data.recode == 9000){
        // message.error("您的权限不足");
      } else if (data.recode == 1001){
        // window.location.hash = "/user/login"
      } else {
        // message.error('网络请求失败!');
      }

      yield put({
        type: 'save',
        payload: {submitting: false}
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

  },
  reducers: {
    save(state, action) {

      return {
        ...state,
        ...action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    clear(state){
      return {
        ...state,
        ...{
          dataList: [],

          loading: true,
          page: 1,
          total: 0,
          pageSize: 10,
          id: 0,

          //字段
          title: '', //标题
          img: '', //图片
          type: 0, //'0 通用 1—内链 2—外链'
          sort: 0, //排序
          imgType:1, //1: 首页轮播图, 2: 待定
          url:'', //轮播图连接url
          submitting: false,
          content:'',
          //图片上传
          previewVisible: false,
          previewImage: '',
          fileList: [],
          fileLists: [],
        },
      };
    },
  },
};
